package com.skywolf.chem.ctconfig.controller;


import com.skywolf.chem.common.enums.ResponseCodeEnum;
import com.skywolf.chem.common.sysconfig.exception.BizException;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.ctconfig.entity.CtConfig;
import com.skywolf.chem.ctconfig.mapper.CtConfigMapper;
import com.skywolf.chem.ctconfig.pojo.CtConfigDto;
import com.skywolf.chem.ctconfig.service.ICtConfigService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 川体生活馆配置 前端控制器
 * </p>
 *
 * @author 
 * @since 2020-09-09
 */
@Slf4j
@RestController
@RequestMapping("/ct/ct-config")
public class CtConfigController {

    @Autowired
    ICtConfigService ctConfigService;

    @Autowired
    CtConfigMapper ctConfigMapper;

    /**
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/jumpUrl", method = {RequestMethod.POST,RequestMethod.GET})
    public void jumpUrl(HttpServletRequest request, HttpServletResponse response)throws BizException{
        try{
            String token = request.getHeader("AUTH-TOKEN");
            String platformType = request.getHeader("platformType");
            String user_id = request.getHeader("user_id");
            String jumpId = request.getParameter("jumpId");
            CtConfig config = ctConfigMapper.selectById(Long.valueOf(jumpId));
            if(config.getLogin()==1 &&
                    (StringUtils.isBlank(token) || StringUtils.isBlank(user_id))){
                throw new BizException(ResponseCodeEnum.INVALID_TOKEN);
            }
            String url = config.getTourl();
            if(token!=null && user_id!=null){
                url = url+"?uid="+user_id+"&token="+token+"&platformType="+platformType;
            }
            log.info("CtConfigController.jumpUrl ,url:{},token:{},uid:{}",url,token,user_id);
            response.sendRedirect(url);
        }catch (BizException e){
            log.error("CtConfigController.jumpUrl ,error:{}",e);
            throw e;
        }catch (Exception e){
            log.error("CtConfigController.jumpUrl ,error:{}",e);
        }
    }

    @ApiOperation(value = "获取生活馆配置",notes = "获取生活馆配置")
    @RequestMapping(value = "/getConfig",method = {RequestMethod.GET,RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<CtConfigDto> getConfig() {
        CtConfigDto list = ctConfigService.getConfig();
        return RespData.success(list);
    }

}
